package com.hqz.service.impl;

import com.baomidou.mybatisplus.core.conditions.AbstractWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hqz.entity.Article;
import com.hqz.entity.Post;
import com.hqz.mapper.ArticleMapper;
import com.hqz.mapper.PostMapper;
import com.hqz.service.ArticleService;
import com.hqz.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Transactional
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService{

    @Autowired
    private ArticleMapper articleMapper;

    @Autowired
    private PostMapper postMapper;

    @Override
    public IPage<Article> selectList(Page<Article> page) {

        return articleMapper.selectList(page);
    }

    @Override
    public Article selectByName(String articleName) {
        return articleMapper.selectByName(articleName);
    }

    @Override
    public Integer add(Article article, Integer typeId) {

        article.setCreateTime(StringUtils.getNowTime());
        article.setUpdateTime(article.getCreateTime());//获取创建时间，可以提高一点性能
        article.setIsDel(0);

        int result = articleMapper.insert(article);

        int articleId = article.getArticleId();//获取插入自增的id
        //将articleId和typeId一同插入到  员工与角色关系表
        Post post = new Post(articleId,typeId);
        post.setCreateTime(StringUtils.getNowTime());
        post.setUpdateTime(post.getCreateTime());//获取创建时间，可以提高一点性能
        post.setIsDel(0);

        postMapper.insert(post);

        return result;
    }

    @Override
    public Integer update(Article article, Integer typeId) {

        article.setUpdateTime(StringUtils.getNowTime());
        int result = articleMapper.updateById(article);

        Post post = new Post();

        post.setArticleId(article.getArticleId());
        post.setTypeId(typeId);

        post.setUpdateTime(StringUtils.getNowTime());

        AbstractWrapper abstractWrapper = new QueryWrapper();
        abstractWrapper.eq("artuicle_id",article.getArticleId());

        postMapper.update(post,abstractWrapper);
        return result;
    }
}
